//
//  MainAccountPopView.swift
//  d11
//
//  Created by peale on 2018/9/19.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit
typealias AccountSelectBlock = (_ index: Int) -> Void
 let  reuseIdentifierHeadID : NSString = "MainAccountPopHeaderView"
let  reuseIdentifierID : NSString = "MainAccountPopViewCell"
class MainAccountPopView: UIView, Modal,UITableViewDelegate,UITableViewDataSource {
    var accountSelectBlock: AccountSelectBlock?
    func callClickBlock(_ block: @escaping AccountSelectBlock) {
        
        accountSelectBlock = block
    }
   var dialogView = UIView()
    
    var backgroundView = UIView()
    var arrayList : [[String:Any]]? //数据列表
    var type : Int = 0
    var selectDict : [String:Any]?
    lazy var tableView:UITableView = {
        let table = UITableView(frame:CGRect(x: 0, y: 0, width: self.width, height:self.height), style:UITableView.Style.plain)
        table.dataSource = self
        table.delegate = self
        //table.layoutMargins = UIEdgeInsets.zero
        table.separatorStyle = .none
        table.backgroundColor  = UIColor.white
        table.showsVerticalScrollIndicator = false
        table.tableFooterView = UIView.init()
        table.separatorStyle = .none
        table.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        table.estimatedRowHeight = 0
        table.estimatedSectionHeaderHeight = 0
        table.estimatedSectionFooterHeight = 0
//        if #available(iOS 11.0, *) {
//            table.contentInsetAdjustmentBehavior = .never
//
//        } else {
//            self.automaticallyAdjustsScrollViewInsets = false
//        }
        return table
    }()
    
    convenience init(list:[[String:Any]]) {
         self.init(frame: UIScreen.main.bounds)
        self.arrayList  = list
        if self.arrayList == nil {
            self.arrayList = [[String:Any]]()
        }
        backgroundView.backgroundColor = UIColor.black
        backgroundView.alpha = 0.6
        backgroundView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTappedOnBackgroundView)))
        addSubview(backgroundView)
        
        backgroundView.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.height.equalToSuperview()
            make.top.equalTo(0)
        }
        
        dialogView  = UIView.init(frame: UIScreen.main.bounds)
        dialogView.backgroundColor = UIColor.init(white: 1, alpha: 1)
        dialogView.layer.cornerRadius = 5;
        dialogView.layer.masksToBounds = true
        addSubview(dialogView)
        
        dialogView.snp.makeConstraints { (make) in
            make.width.equalTo(300)
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(kScreenHeight*0.6)
        }
        
        dialogView.addSubview(self.tableView)
  //      self.tableView.frame = CGRect(x: 0, y: 0, width: 300, height:kScreenHeight*0.6)
        
        tableView.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.top.equalToSuperview()
            make.left.equalToSuperview()
//            make.centerY.equalToSuperview()
//            make.centerX.equalToSuperview()
            make.height.equalToSuperview()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func didTappedOnBackgroundView(){
       // UIHelper.removeEffect()
        dismiss(animated: true)
    }
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  self.arrayList!.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
       return 44
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        var headerView : MainAccountPopHeadView? =  (tableView.dequeueReusableHeaderFooterView(withIdentifier: reuseIdentifierHeadID as String) as? MainAccountPopHeadView)
        if headerView == nil {
            headerView = MainAccountPopHeadView(reuseIdentifier: reuseIdentifierHeadID as String)
        }
        if type == 1 {
            headerView?.setData(isOut: true)
        }else{
            headerView?.setData(isOut: false)
        }
         return  headerView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell :MainAccountPopViewCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierID as String) as? MainAccountPopViewCell
        if cell == nil {
            cell =  MainAccountPopViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifierID as String)
        }
       let dict = self.arrayList?[indexPath.row]
       let dAccid = dict?["id"] as? String ?? "-1"
        
        var isSelect = false
        if self.selectDict != nil{
            let sAccid = selectDict?["id"] as? String ?? "-2"
           if dAccid == sAccid {
                isSelect = true
            }
        }
        cell?.setData(dict: dict, isSelect: isSelect)
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         self.didTappedOnBackgroundView()
        if accountSelectBlock != nil {
            accountSelectBlock!(indexPath.row)
        }
            
    }
}
